
"use strict";
/* Controllers */
app.controller("dataMetricListController", ['$scope', '$location', '$rootScope', '$routeParams', 'dataMetricListService', function ($scope, $location, $rootScope, $routeParams, dataMetricListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.d = {};
    vm.result = {};
    vm.p.biz = $routeParams.biz;

    vm.d.color = {};
    vm.d.color[vm.p.biz] = 'red';
    vm.d.bizTxt = (vm.p.biz=='zp')?'HRG':'赶集';
    vm.p.bizArea = bizAreaTran(vm.p.biz);



    var errorFn = function (result) {
        alert(result);
    };
    $scope.changePage = function(pageName) {
        $location.path('/star/metric2/data_metric_list/' + pageName);
        $location.replace();
    };

    $scope.searchItems = function() {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 去除两端空格
        vm.p.search = vm.p.search.replace(/^\s+|\s+$/g,'');
        if(vm.p.search=='') {
            alert("请输入搜索表名称");
            return;
        }
        var params = {
            search: vm.p.search,
            bizArea: vm.p.bizArea
        };
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            //vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        dataMetricListService.searchMetric(params, procFn, errorFn);
    };

    $scope.listMetric = function (topic) {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {
            bizArea: vm.p.bizArea,
            topic: topic
        };
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            //vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
//            listUserSubscribe();
//            var tbl = $('#list_table');
//            tbl.bootstrapTable('load', data);
//            $compile(tbl)($scope);
            $scope.listUserSubscribe();

            $("#list_table").on("click","[data-subscribe]",function(){
            var vm = $scope.vm;
            var params = {};
            var subscribe = $(this)
            var id = subscribe.attr("data-subscribe")
            params.id=id
            var procFn = function (response) {
                if (response.success) {
                    subscribe.hide()
                    $("[data-cancelSubscribe="+id+"]").show()
                } else {
                    alert(response.msg);
                }
            };
            dataMetricListService.subscribe(params,procFn,errorFn)
        })

        $("#list_table").on("click","[data-cancelSubscribe]",function(){
            var vm = $scope.vm;
            var params = {};
            var cancelSubscribe=$(this)
            var id = cancelSubscribe.attr("data-cancelSubscribe")
            params.id=id
            var procFn = function (response) {
                if (response.success) {
                    cancelSubscribe.hide()
                    $("[data-subscribe="+id+"]").show()
                } else {
                    alert(response.msg);
                }
            };
            dataMetricListService.cancelSubscribe(params,procFn,errorFn)

        })
        };
        dataMetricListService.listMetric(params, procFn, errorFn);
    };

    $scope.listMetricRouter = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {
            bizArea: vm.p.bizArea
        };
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
//            vm.conf = [];
//            // 匹配节点
//            var findText = function(arr, text) {
//                var idx = -1;
//                for(var i=0;i<arr.length;++i) {
//                    if(arr[i].text==text) {
//                        idx = i;
//                        break;
//                    }
//                }
//                return idx;
//            };
//            var addNode = function(tree, node1, leaf) {
//                var node1Obj;
//                var node1Idx = findText(tree, node1);
//                if(node1Idx==-1) {
//                    node1Obj = {text: node1, nodes:[]};
//                    tree.push(node1Obj);
//                }else {
//                    node1Obj = tree[node1Idx];
//                }
//                node1Obj.nodes.push(leaf);
//            };
//            for(var i=0; i<data.length; ++i) {
//                addNode(vm.conf,data[i].topicGroup, data[i].topic);
//            }
//
            vm.conf = list2Tree(data);

        };
        dataMetricListService.listMetricRouter(params, procFn, errorFn);
    };

    $scope.queryItems = function(topic) {
        var vm = $scope.vm;
        $scope.listMetric(topic);
    };

    $scope.listUserSubscribe = function () {
        var params={}
        var procFn = function (response) {
            if (response.success) {
                var res=response.data
                for(var index in response.data){
                    var metricId=res[index].metricId
                    $("[data-subscribe="+metricId+"]").hide()
                    $("[data-cancelSubscribe="+metricId+"]").show()
                }
            } else {
                alert(response.msg);
            }
        };
        dataMetricListService.listUserSubscribe(params,procFn,errorFn);
//        return dataMetricService.subscribe(params, procFn, errorFn);
    };


    $scope.initializeController = function () {
        var vm = $scope.vm;

        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [10, 30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.listMetricRouter();
        $scope.listMetric(null);

        $resultTable.bootstrapTable('hideColumn', 'topic');
        $resultTable.bootstrapTable('hideColumn', 'indexEn');
        $resultTable.bootstrapTable('hideColumn', 'appDiv');
        $resultTable.bootstrapTable('hideColumn', 'zgroup');
        $resultTable.bootstrapTable('hideColumn', 'zlevel');
    };

    $scope.initializeController();



}]);


/* Services */
app.service('dataMetricListService', ['ajaxService', function (ajaxService) {

    this.listMetricRouter = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/listMetricRouter.do", successFunction, errorFunction);
    };

    this.listMetric = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/listMetric.do", successFunction, errorFunction);
    };

    this.searchMetric = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/searchMetric.do", successFunction, errorFunction);
    };

    this.subscribe = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/subscribe.do", successFunction, errorFunction);
    };

    this.cancelSubscribe = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/cancelSubscribe.do", successFunction, errorFunction);
    };

    this.listUserSubscribe = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/listUserSubscribe.do", successFunction, errorFunction);
    };
}]);

var bizAreaTran = function(biz) {
    var area = '';
    switch(biz) {
        case 'zp':
            area = 'HRG指标';
            break;
        case 'ganji':
            area ='赶集指标';
            break;
        case '58':
            area = '58指标';
            break;
        case 'chinahr':
            area = '英才指标';
            break;
        default:
            break;
    }
    return area;
};

function statusOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    switch (value) {
        case 0:
//            return "未开发";
        case 1:
//            return "开发中";
        case 2:
//            return "验证中";
        case 3:
            return "待上线";
        case 4:
            return "已上线";
        case 11:
//            return "下线中";
        case 12:
//            return "待下线";
        case 13:
            return "已下线";
        case 21:
//            return "升级中";
        case 22:
//            return "待升级";
        default:
            return "-";
    }
}

function dataMetricIDOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var dhtml=[
        '<a href="#/star/metric2/data_metric_add/', row.biz,'/',row.id,'" target="_blank"><u>', "修改", '</u></a>',
//        '<a href="#/star/metric2/data_metric_detail/', row.biz,'/',row.id,'" target="_blank">', " <u>查看</u>", '</a>',
        '<a href="javascript:void(0)" data-subscribe="',value,'">', " <u>关注</u>" , '</a>',
        '<a href="javascript:void(0)" style="display: none;" data-cancelSubscribe="',value,'">', " <u>已关注</u>" , '</a>'
    ].join('')

    return dhtml;
}

function indexStdFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var val=[
        '<a href="#/star/metric2/data_metric_detail/', row.biz,'/',row.id,'" target="_blank"><u>', value , '</u></a>',
    ].join('')

    return val;
}

function businessSystemFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return row.userJourneyMap+'-'+row.businessSystem;
}

function indexDefineFmt(value, row, index) {
    if (value == null) {
            return "-";
    }
    var items = value.split(/,|，|。/);
    value = items[0];

    var result = [];
    var MAX_LEN= 30;
    for(var i=0;i<value.length;) {
        var item = value.substr(i, MAX_LEN);
        result.push(item);
        i += MAX_LEN;
    }
    return result.join('<br/>');
}




var subscribe = function (id) {
    alert('已关注: '+id);
};
